package com.woniu.woniuticket.untils;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.woniu.woniuticket.config.AlipayConfig;
import com.woniu.woniuticket.entity.OrderForm;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class AliPay {

    public static String getAliPayOrderStr(OrderForm orderTest) {
        //调用支付宝请求
        AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE);
        AlipayTradeWapPayRequest request=new AlipayTradeWapPayRequest();
        //封装请求信息
        AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
        log.info("订单编号："+orderTest.getOrderNo());
        model.setOutTradeNo(orderTest.getOrderNo());
        log.info("订单名称："+orderTest.getOrderName());
        model.setSubject(orderTest.getOrderName());
        log.info("订单金额："+orderTest.getOrderPrice());
        model.setTotalAmount(orderTest.getOrderPrice()+"");
        model.setProductCode("QUICK_WAP_WAY");
        //设置回传参数
        String passBackParams="orderId="+orderTest.getOrderId();
        model.setPassbackParams(passBackParams);
        request.setBizModel(model);
        // 设置同步地址
        request.setReturnUrl(AlipayConfig.return_url);
        // 设置异步地址
        request.setNotifyUrl(AlipayConfig.notify_url);
        //输出页面的表单
        String webForm = "";
        try {
            //调用SDK生成表单
            webForm = client.pageExecute(request).getBody();
        } catch (Exception e) {
            log.info("支付请求发送失败");
            return "fail";
        }
        log.info(webForm);
        return webForm;
    }
}
